package com.tech.platform.cms.admin.service;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.tech.platform.cms.authz.client.service.SsoClientService;
import com.tech.platform.cms.authz.common.exception.GetTokenException;
import com.tech.platform.cms.authz.common.model.LoginUserModel;
import com.tech.platform.cms.authz.common.utils.JwtUtils;
import com.tech.platform.cms.common.constant.Constant;
import com.tech.platform.cms.common.model.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
@Slf4j
public class LoginService {

    @Autowired
    private SsoClientService ssoClientService;

    /**
     * 到认证服务器获取系统hgt token
     * @param httpServletRequest
     * @return
     */
    public CommonResult<LoginUserModel> getHgtToken(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){

        CommonResult<String> tokenResult = ssoClientService.authorizationByCookie(httpServletRequest,httpServletResponse);
        if(tokenResult.getCode()==Constant.SUCCESS){
            //hgt token 放入cookie
            String token = tokenResult.getData();
            LoginUserModel model = ssoClientService.getLoginUser(token);
            return new CommonResult<>(Constant.SUCCESS,"success",model);
        }
        throw new GetTokenException( Constant.AUTHORIZATIONERROR,"token 验证失败");
    }

}
